package com.android.binderserverclient0.buryingpoint

import android.util.Log
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

class OnClickListenerProxy(target: Any) : InvocationHandler {

    private val mTarget: Any = target

    override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
        return if ("onClick" == method?.name) {
            Log.d("OnClickListenerProxy","fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?)")
            val field = args?.get(0)
            if (field is View) {
                Log.d("OnClickListenerProxy","proxy is View---${field.contentDescription}")
            }
            method.invoke(mTarget, *args.orEmpty())
        } else {
            return null
        }
    }

    fun getProxyInstance() : Any? {
        return Proxy.newProxyInstance(
            mTarget.javaClass.classLoader,
            mTarget.javaClass.interfaces,
            this
        )
    }
}